import {defineStore} from 'pinia'
import {
    requestArticleList,
    requestArticleAction,
    requestArticleBehavior,
    requestArticleDelete,
    requestArticleGetBehaviorList,
    requestArticleInsert,
    requestArticleUpdate,
    requestArticleDetail,
    requestArticleGetArticleLabel,
    ARTICLE_Label,
    ARTICLE_TYPE,
    ARTICLE_ACTION,
    ARTICLE_INFO,
    BehaviorList
} from '../service/article'
import {reactive, ref} from 'vue'


export default defineStore('useArticleStore', () => {
    const articleListData = reactive({
        objects: [],
        total: 0,
        totalPage: 0,
        pageNo: 1,
        pageSize: 10
    })

    const personalListData = reactive({
        objects: []
    })

    const articleDetailData = reactive({
        details: []
    })

    const pageResults = reactive({
        data: {}
    })

    // 文章点赞，访问，收藏
    const articleAction = async (articleAction: Partial<ARTICLE_ACTION>) => {
        const res = await requestArticleAction(articleAction)
        console.log(res, "｜ res");
    }

    // 文章点赞，收藏， 评论行为收集
    const articleBehavior = async (articleBehavior: Partial<ARTICLE_ACTION>) => {
        const res = await requestArticleBehavior(articleBehavior)
        // console.log(res, "｜ res");
        return res
    }

    // 删除文章
    const articleDelete = async (delId: { id: number }) => {
        const res = await requestArticleDelete(delId)
        console.log(res, "｜ res");
    }

    // 获取文章详情
    const articleDetail = async (detailData: { id: number }) => {
        const res = await requestArticleDetail(detailData)
        articleDetailData.details = res?.data
    }

    // 文章标签
    const articleLabel = async (detailLabel: Partial<ARTICLE_Label>) => {
        const res = await requestArticleGetArticleLabel(detailLabel)
        return res
    }

    // 获取登录用户点赞，评论，收藏行为
    const articleGetBehaviorList = async (behaviorList: Partial<BehaviorList>) => {
        const res = await requestArticleGetBehaviorList(behaviorList)
        // console.log(res, "｜ res");
        // return res?.data
        pageResults.data = res?.data
    }

    // 新增文章
    const articleInsert = async (articleInfo: Partial<ARTICLE_INFO>) => {
        const res = await requestArticleInsert(articleInfo)
        console.log(res, "｜ res");
    }

    // 获取文章列表
    const articleList = async (articleDetail: Pick<ARTICLE_TYPE, 'latitude' | 'longitude' | 'page' | 'pageSize' | 'source'>, init: string) => {
        if (init) {
            articleListData.objects = []
            articleListData.total = 0
            articleListData.totalPage = 0
            articleListData.pageNo = 1
        }
        const res = await requestArticleList(articleDetail)
        personalListData.objects = res?.data?.objects
        // articleListData.objects = res?.data?.objects
        articleListData.objects = articleListData.objects.concat(res?.data?.objects)
        articleListData.total = res?.data?.total
        articleListData.totalPage = res?.data?.totalPage
        articleListData.pageNo = res?.data?.pageNo
    }

    // 更新文章
    const articleUpdate = async (articleInfo: Partial<ARTICLE_INFO>) => {
        const res = await requestArticleUpdate(articleInfo)
        console.log(res, "｜ res");
    }

    return {
        articleAction,
        articleBehavior,
        articleDelete,
        articleDetail,
        articleGetBehaviorList,
        articleInsert,
        articleList,
        articleUpdate,
        articleListData,
        articleDetailData,
        pageResults,
        articleLabel,
        personalListData
    }
})